package sicau.carManager.Util.TokenUtil;

import sicau.carManager.entity.Admin;

import java.util.HashMap;

/**
 * @Author ITcz
 * @Data 2020-09-28 - 16:22
 */

public class TokenUtil {
    /**
    * HashMap中Key为Token，Value为账户信息
    * */
    private static HashMap<String, Admin> tokenMap = new HashMap();

    /**
     *前端在请求头里传token,JSON数据
     */
    public static final String ADMIN_TOKEN = "token";

//    public static String setToken(String campus, Integer authority){
//        //生成一段随机值并赋值给token
//        String token = UUIDUtil.uuid();
//        //将token和管理员的校区码放入map里
//        Admin admin = new Admin();
//        admin.setCampus(campus);
//        admin.setAuthority(authority);
//        tokenMap.put(token, admin);
//        return token;
//    }
    public static String setToken(Admin admin){
        //生成一段随机值并赋值给token
        String token = UUIDUtil.uuid();
        //将token和管理员的信息放入map里
        tokenMap.put(token, admin);
        return token;
    }

    public static Admin getAdminByToken(String token){
        return (Admin) tokenMap.get(token);
    }

    public static boolean containsToken(String token) {
        return tokenMap.containsKey(token);
    }

    public static void removeToken(String token){
        tokenMap.remove(token);
    }

    public static void showAll(){
        for (String key:
        tokenMap.keySet()
             ) {
            System.out.println(key +":"+ tokenMap.get(key));
        }
    }

    public static void deleteAll(){
        System.out.println("clean all token msg...");
        tokenMap.clear();
    }
}
